package com.campus.counseling.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.counseling.model.common.Result;
import com.campus.counseling.entity.Appointment;
import com.campus.counseling.model.dto.AppointmentDTO;
import com.campus.counseling.model.dto.AppointmentQuery;
import com.campus.counseling.model.dto.CancelDTO;
import com.campus.counseling.model.vo.AppointmentVO;
import com.campus.counseling.service.AppointmentService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Tag(name = "预约管理")
@RestController
@RequestMapping("/api/counseling/appointment")
@RequiredArgsConstructor
public class AppointmentController {

    private final AppointmentService appointmentService;

    @ApiOperation("获取预约列表")
    @GetMapping("/list")
    public Result<Page<AppointmentVO>> list(AppointmentQuery query) {
        Page<AppointmentVO> page = appointmentService.getAppointmentList(query);
        return Result.success(page);
    }

    @ApiOperation("创建预约")
    @PostMapping("/create")
    public Result<Void> create(@RequestBody AppointmentDTO appointmentDTO) {
        appointmentService.createAppointment(appointmentDTO);
        return Result.success();
    }

    @ApiOperation("更新预约")
    @PutMapping("/update")
    public Result<Void> update(@RequestBody AppointmentDTO appointmentDTO) {
        appointmentService.updateAppointment(appointmentDTO);
        return Result.success();
    }

    @PutMapping("/confirm/{id}")
    @ApiOperation("确认预约")
    public Result<Void> confirm(@PathVariable Long id) {
        appointmentService.confirmAppointment(id);
        return Result.success();
    }

    @PutMapping("/cancel/{id}")
    @ApiOperation("取消预约")
    public Result<Void> cancel(@PathVariable Long id, @RequestBody CancelDTO cancelDTO) {
        appointmentService.cancelAppointment(id, cancelDTO.getReason());
        return Result.success();
    }

    @ApiOperation("获取预约详情")
    @GetMapping("/{id}")
    public Result<AppointmentVO> detail(@PathVariable Long id) {
        return Result.success(appointmentService.getAppointmentDetail(id));
    }

    @PutMapping("/revoke/{id}")
    @ApiOperation("撤回取消的预约")
    public Result<Void> revoke(@PathVariable Long id) {
        appointmentService.revokeAppointment(id);
        return Result.success();
    }

    @Operation(summary = "获取指定日期范围内的预约记录")
    @GetMapping("/date-range")
    public Result<List<Appointment>> getAppointmentsByDateRange(
            @RequestParam(required = false) Long counselorId,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        List<Appointment> appointments = appointmentService.getAppointmentsByDateRange(counselorId, startDate, endDate);
        return Result.success(appointments);
    }

    @GetMapping("/available-slots")
    @Operation(summary = "获取可预约时间段")
    public Result<List<Map<String, Object>>> getAvailableTimeSlots(
            @RequestParam Long counselorId,
            @RequestParam String date) {
        LocalDate localDate = LocalDate.parse(date);
        return Result.success(appointmentService.getAvailableTimeSlots(counselorId, localDate));
    }

    @GetMapping("/trend")
    @ApiOperation("获取预约趋势统计")
    public Result<Map<String, Object>> getAppointmentTrend(@RequestParam Integer days) {
        Map<String, Object> trendData = appointmentService.getAppointmentTrend(days);
        return Result.success(trendData);
    }
} 